<?php
/**
 * 《我的资料》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/../emp/Info.class.php');

class Profile extends Info {

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}
	
	function layout($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'my/profile/layout');
		return true;
	}

	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-7 day'));

		$controller->display($request, 'my/finance/salary/find');
		return true;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
		
		$emp = $_SESSION['id'];
		
		return " emp = '" . $emp . "'";
	}
	
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				
				$row['oper_t'] = Name::emp($row['oper']);
				$row['status_t'] = $this->status[$row['status']];

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'my/finance/salary/list');
		return true;
	}
	
	function auth($controller, $request) {
		
		if ($_SESSION['password_2']) {
			$data = array('status'=>'1');
		} else {
			$data = array('status'=>'2');
		}
		
		echo json_encode($data);
	}
	
	function login($controller, $request) {		
		$password = $request['password'];
		
		$emp = $_SESSION['id'];
		$s = $this->password($emp, '2');
		debug($s);
		debug(print_r($request, true));
		
		if (!empty($password) && $password == $s) {
			$data = array('status'=>'1');
			$_SESSION['password_2'] = true;
		} else {
			$data = array('status'=>'2');
		}
		
		echo json_encode($data);
	}
	
	function password($emp, $type) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND type = '%s'";
		$sql = sprintf($fmt, Constant::tbl_emp_password, $emp, $type);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			return $row['password'];
		}
		
		return '';	
	}
	
	function _view($controller) {
		$this->pretreat($controller);
		
		$id = $_SESSION['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);
		
		$row['dept_t'] = $this->_dept($row['dept']);

		$row['sex'] = $this->sex[$row['sex']];
		$row['education'] = $this->education[$row['education']];
		$row['marriage'] = $this->marriage[$row['marriage']];
		$row['nationality'] = $this->nationality[$row['nationality']];

		$controller->assign_by_ref('emp', $row);
		$controller->assign_by_ref('result', $row);
	}
	
	function base($controller, $request) {
		$this->_view($controller);

		$controller->display($request, 'my/profile/base');
		return true;
	}
	
	function info($controller, $request) {
		$this->_view($controller);
		
		$controller->display($request, 'my/profile/info');
		return true;
	}
	
	function contract($controller, $request) {
		$id = $_SESSION['id'];	
			
		$controller->assign_by_ref('contract', $this->_my_additional(Constant::tbl_emp_contract, $id));
		
		$controller->display($request, 'my/profile/contract');
		return true;
	}
	
	function bank($controller, $request) {
		$id = $_SESSION['id'];
		
		$controller->assign_by_ref('bank', $this->_bank($id));
		
		$controller->display($request, 'my/profile/bank');
		return true;
	}
	
	function _bank($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE info = '1' AND info_id = '%s' AND status = '1'";
		$sql = sprintf($fmt, Constant::tbl_cw_bank, $id);
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['code'] = $this->_format($row['code'], 4, ' ');
			
			$ret[] = $row;
		}
		
		return $ret;
	}
	
	function family($controller, $request) {	

		$id = $_SESSION['id'];	
			
		$controller->assign_by_ref('family', $this->_my_additional(Constant::tbl_emp_family, $id));

		$controller->display($request, 'my/profile/family');
		return true;
	}
	
	function education($controller, $request) {
		$this->pretreat($controller);

		$id = $_SESSION['id'];
		
		$controller->assign_by_ref('education', $this->_my_additional(Constant::tbl_emp_education, $id));

		$controller->display($request, 'my/profile/education');
		return true;
	}
	
	function work($controller, $request) {
		$this->pretreat($controller);

		$id = $_SESSION['id'];
		
		$controller->assign_by_ref('work', $this->_my_additional(Constant::tbl_emp_work, $id));

		$controller->display($request, 'my/profile/work');
		return true;
	}
}
?>